package 力扣._00周赛._375周赛.问题3;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public long countSubarrays(int[] nums, int k) {
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            max = Math.max(max, nums[i]);
        }
        int max_k = 0;    //当前窗口最大值元素的出现次数
        //滑动窗口
        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;  //满足要求的子数组个数
        int left = 0, right = 0; //left开，包含r
        while (right < nums.length) {

            //将当前元素(r指向的)加入
            if (nums[right] == max) {
                max_k++;
                if (max_k == k) { //加入该元素后符合,则以当前的lfet开始的子数组有len - right
                    //left右移，知道为max
                    while (nums[left] != max) {
                        count += nums.length - right;
                        left++;
                    }
                    count += nums.length - right;
                    left++;
                    max_k--;
                }
            }
            right++;
        }
        return count;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 3, 2, 3, 3};
        int k = 2;
        new Solution().countSubarrays(nums, k);
    }
}